<template>
  <div class="single">
    <el-row>
      <el-col :span="24">
        <el-form
          :model="formModel"
          :rules="rules"
          ref="formModel"
          label-width="110px"
          class="demo-formModel"
        >

          <!-- 聘任专业技术职务 -->
          <el-row>
            <el-col :span="8">
              <el-form-item label="聘任专业技术职务" prop="pzjszw">
                <el-input v-model="formModel.pzjszw" placeholder="请输入聘任专业技术职务" class="el_select"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="聘任开始年月" prop="pzkssj">
                <el-date-picker
                  type="date"
                  placeholder="请选择聘任开始年月"
                  v-model="formModel.pzkssj"
                  value-format="yyyy-MM-dd"
                  style="width: 15rem"
                ></el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="聘任结束年月" prop="pzjssj">
                <el-date-picker
                  type="date"
                  placeholder="请选择聘任结束年月"
                  value-format="yyyy-MM-dd"
                  v-model="formModel.pzjssj"
                  style="width: 15rem"
                ></el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row class="btns">
            <el-col :span="24">
              <el-form-item>
                <el-button type="primary" @click="submitForm('formModel')"
                >{{ btnname }}
                </el-button
                >
                <el-button @click="resetForm('formModel')">重置</el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script>
export default {
  name: "jobinfo",
  data() {
    return {
      cityList: [
        {name: "中国", val: 0},
        {name: "香港", val: 1},
      ],
      educationList: [
        {name: "小学", val: 0},
        {name: "大学", val: 1},
      ],
      schoolList: [
        {name: "xxx学院", val: 0},
        {name: "xxx学院", val: 1},
      ],

      imageUrl: '',
      formModel: {
        jsId: "",
        pzjszw: "",
        pzkssj: "",
        pzjssj: "",

      },
      rules: {
        pzjszw: [
          {required: true, message: "请输入聘任专业技术职务", trigger: "blur"},
        ],

        pzkssj: [
          {
            type: "string", required: true, message: "请选择聘任开始年月", trigger: "change",
          },
        ],
        pzjssj: [
          {
            type: "string", required: true, message: "请选择聘任结束年月", trigger: "change",
          },
        ],

      },
      btnname: '添加',
      page: {
        id: '',
        op: 'add'
      },
    };
  },
  mounted () {
    this.initdata()
  },
  methods: {
    initdata () {
      var params = this.$route.params
      console.log(params)
      this.page.id = params.id
      this.page.op = params.op
      this.get_detail()
    },
    get_detail () {
      var params = {jsId: this.page.id}
      this.$http({
        url: this.$http.adornUrl('/zyjszwprxx/getJsZyjszwprxx'),
        method: 'get',
        params: this.$http.adornParams(params)
      }).then(({data}) => {
        console.log(data.data)
        if (data && data.code === 0) {
          if (data.data.length > 0) {
            this.page.op = 'detail'
            this.btnname = '修改'
            this.formModel = data.data[0]
          } else {
            this.page.op = 'add'
            this.btnname = '添加'
          }
        } else {
          this.$message.error(data.msg)
        }
      })
    },
    submitForm(formName) {
      this.formModel.jsId = this.$store.state.user.jsid
      let formData = this.get_formdata(this.formModel);
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = ''
          if (this.page.op == 'add') {
            url = '/zyjszwprxx/save'
          } else {
            url = '/zyjszwprxx/update'
          }
          this.$http({
            url: this.$http.adornUrl(url),
            data: formData,
            method: 'post'
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.$nextTick(() => {
                    if (this.page.op == 'add') {
                      this.$refs[formName].resetFields();
                    }
                  })
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    get_formdata (config) {
      console.log(config)
      //对象转formdata格式
      const formData = new FormData()
      //循环传入的值转换formData
      Object.keys(config).forEach((key) => {
        formData.append(key, config[key])
      })
      return formData
    },
  },
};
</script>


<style>
.single {
  padding: 0.5rem;
}

.el_select {
  width: 15rem;
}

.btns {
  padding-left: 22rem;
}

</style>

